package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.been.entity.Users;
import com.woniu.mapper.UsersMapper;
import com.woniu.service.UserService;
import com.woniu.utils.PageResult;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;


    @Override
    public PageResult findAll(Users users, Integer pageNum, Integer pageSize) {
        //设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法
        List<Users> list = usersMapper.findAll(users);
        //创建PageInfo对象
        PageInfo<Users> pageInfo = new PageInfo<>(list);
        //封装响应数据
        PageResult ps = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return ps;
    }

    @Override
    public Users login(String userName, String password) {
        return usersMapper.login(userName,password);
    }


    @Override
    public void register(Users users) {
         usersMapper.register(users);
    }

    @Tool(description = "创建一个用户", name = "addUser")
    public void addUser(@ToolParam(description = "新增用户实体类,包含用户姓名，密码，角色，余额，积分，头像，创建人，创建时间(当前时间time)，修改人，修改时间，手机号" )
                            Users users) {
        usersMapper.register(users);
    }

    @Override
    public void update(Users users) {
        usersMapper.update(users);
    }

    @Override
    public void deleteById(Integer userId) {
        usersMapper.deleteById(userId);
    }

    @Override
    public Users findById(Integer userId) {
        return usersMapper.findById(userId);
    }

    @Override
    public void updateById(Users user) {
        usersMapper.updateById(user);
    }
}
